using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_HO_ManagementBranch : System.Web.UI.Page
{
    func fn = new func();
    string rID = null;
    protected void Page_Load(object sender, EventArgs e)
    {//kiem tra request act
        if (Request.QueryString["act"] != null) {
            
            switch (Request.QueryString["act"].ToString()) { 
                case"del":
                    
                    if(Request.QueryString["rID"]!=null){
                       
                        rID = Request.QueryString["rID"].ToString();
                        if (fn.RoutesCheckExistByID(rID) == true)
                        {
                             
                            string sql = "delete  BranchesOnRoute where RouteID='" + rID + "';"+"delete from Routes where RouteID='" + rID + "'";
                            if (fn.EXECQUERY(sql) > 0)
                            {
                                lblthongbao.Text = "Deleted";
                            }
                            else {

                                lblthongbao.Text = "Cannot delete routes";
                            }
                            
                            

                        }
                        else {
                            lblthongbao.Text = "Routes ID not exist!";
                        }
                    }
                    break;
                 
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {//mo panel create
        pnCreate.Visible = true;
        //hide nut update
        btnUpdate.Visible = false;
        pnList.Visible = false;
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //doi ten form
        lblTitleForm.Text = "Edit Routes ";
        pnCreate.Visible = true;
        btnUpdate.Visible = true;
        btnCreate.Visible = false;
        pnList.Visible = false;
        
        if (IsPostBack)
        {//load info
            string rID = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
            DataRow dr = fn.GETDATAROWS("select * from Routes where RouteID='" + rID + "'");
            lblID.Text = dr["RouteID"].ToString();
             
            txtStart.Text = dr["StartPoint"].ToString();
            txtEnd.Text = dr["EndPoint"].ToString();
            
        }
        


    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string sql = "update Routes set StartPoint='"+txtStart.Text+"', EndPoint='"+txtEnd.Text+"' where RouteID='"+lblID.Text+"' ";
        
        if (fn.RoutesCheckExistByID(lblID.Text) == true)
        {
           if (fn.EXECQUERY(sql) > 0)
           {
               lblthongbao.Text = "Updated!";
           }
           else {
               lblthongbao.Text = "Update false!";
           }

        }
        else {
            lblthongbao.Text = "Update false!";
        }



         
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        string sql = "INSERT INTO Routes VALUES('"+txtStart.Text+"', '"+txtEnd.Text+"')";
        if (fn.EXECQUERY(sql)>0)
        {
            lblthongbao.Text = "Created!";
        }
        else {
            lblthongbao.Text = "Cannot create!";
        }
        
    }
}
